#!terry-hardin\main.py
#
# Copyright (C) 2009 Terry Hardin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Sheba Praetorius Nobly aWare!



from google.appengine.api import users
from google.appengine.ext import db
from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document
import datetime
import random
import re

BASE_URL = "http://magic-8ball.appspot.com/assets/"
RESP = [{'text':'As I see it, yes','image':'aisiy.jpg'},
    {'text':'It is certain','image':'iic.jpg'},
    {'text':'It is decidedly so','image':'iids.jpg'},
    {'text':'Most likely','image':'ml.jpg'},
    {'text':'Outlook good','image':'og.jpg'},
    {'text':'Signs point to yes','image':'spty.jpg'},
    {'text':'Without a doubt','image':'wad.jpg'},
    {'text':'Yes','image':'y.jpg'},
    {'text':'Yes - definitely','image':'yd.jpg'},
    {'text':'You may rely on it','image':'ymroi.jpg'},
    {'text':'Reply hazy, try again','image':'rhta.jpg'},
    {'text':'Ask again later','image':'aal.jpg'},
    {'text':'Better not tell you now','image':'bntyn.jpg'},
    {'text':'Cannot predict now','image':'cpn.jpg'},
    {'text':'Concentrate and ask again','image':'caaa.jpg'},
    {'text':'Don\'t count on it','image':'dcoi.jpg'},
    {'text':'My reply is no','image':'mrin.jpg'},
    {'text':'My sources say no','image':'mssn.jpg'},
    {'text':'Outlook not so good','image':'onsg.jpg'},
    {'text':'Very doubtful','image':'vd.jpg'},
    {'text':'Go ask Google!','image':'gag.jpg'}]

def OnRobotAdded(properties, context):
    #Invoked when the robot has been added.
    root_wavelet = context.GetRootWavelet()
    root_wavelet.CreateBlip().GetDocument().SetText('''\n~~~~~~~~~~~~\n
    Magic 8-Ball knows all, sees all!\n
    Ask your question in the following format:\n
    magic 8 ball should i program another robot?\n
    and receive your answer.\n
    \n~~~~~~~~~~~~~~~''')

def OnBlipSubmitted(properties, context):
    blip = context.GetBlipById(properties['blipId'])
    doc = blip.GetDocument()
    contents = blip.GetDocument().GetText()
    if(re.search(r'[Mm]agic[\s-]{0,1}8[\s-]{0,1}[Bb]all', contents)):
        reply = RESP[random.randrange(0,20)] #lists are zero-based
        reply_img = document.Image(BASE_URL + reply['image'])
        #blip.GetDocument().Append('\n\n')
        blip.GetDocument().AppendElement(reply_img)

if __name__ == '__main__':
    myRobot = robot.Robot('magic-8ball', 
        image_url='http://magic-8ball.appspot.com/assets/m8b.jpg',
        version='1',
        profile_url='http://magic-8ball.appspot.com/')
    myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
    myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted)
    myRobot.Run()